import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:blog_chyk_intl/app/lang/index.dart';

/// 拿到下一个可用 locale（循环）
Locale _nextLocale() {
  final locales = supportedLocales; // 来自 lang_loader
  final current = Get.locale ?? const Locale('en');
  final index = locales.indexWhere(
    (e) => e.languageCode == current.languageCode,
  );
  return locales[(index + 1) % locales.length]; // 循环切
}

/// 一键切换（不限两种）
void toggleLocale() {
  final newLocale = _nextLocale();
  Get.updateLocale(newLocale);
  GetStorage().write('locale', newLocale.toString());
}

/// 初始语言（main 里用）
Locale getInitialLocale() {
  final code = GetStorage().read('locale') ?? 'en';
  return Locale(code);
}
